//一键开启、关闭log  true开启 false关闭
console.log = true ? console.log : () => {};
console.info = true ? console.info : () => {};
const {
  URL,
  routes
} = require("/util/router")
const {
  storage
} = require("/util/public")
const product = require("/api/product")
const user = require("/api/user")
const order = require("/api/order")
const other = require("/api/other")
App({
  // 数据接口
  product: product, // 商品接口
  user: user, // 商品接口
  order: order, // 订单接口
  other: other, // 其他页面接口
  globalData: {
    noti: null,
    query: null,
    promotionAccount: '',
    flowState: '',
    // number:'13656666668', // 平台手机号
    number: '13856544147', // 平台手机号
    userlocation: null,
    address: "安徽省合肥市瑶海区明光路恒大中央广场40栋A座1718室",
    company_name: "合肥盛泽尊享网络科技有限公司",
    regional: "安徽省合肥市瑶海区",
    person: "姜焰",

    productId: null, // 首月一元租商品

    catePage_typeIndex: 0
  },

  onLaunch(options) {
    // 第一次打开
    // options.query == {number:1}
    console.info('App onLaunch')
    const promotionAccount = storage('promotionAccount')
    console.log('promotionAccount', promotionAccount)
    const time = parseInt(new Date().getTime() / 1000)
    const DayTime = (time, n = 1) => (time + n * 24 * 60 * 60);
    if (promotionAccount && promotionAccount.time > time) this.globalData.promotionAccount = promotionAccount.value;
    if (options.query) {
      this.globalData.query = options.query
      this.globalData.promotionAccount || (this.globalData.promotionAccount = options.query.promotionAccount ? options.query.promotionAccount : null)
      this.globalData.flowState = options.query.flowState ? options.query.flowState : null

      options.query.promotionAccount && other.qrCodeCount(options.query.promotionAccount)

      if (!options.query.promotionAccount) return false;
      if (promotionAccount && promotionAccount.time > time) return false;
      console.log("开始缓存")
      storage('promotionAccount', {
        value: this.globalData.promotionAccount,
        time: DayTime(time)
      })
    }
    // 获取通知信息
    this.getNotice()
  },
  onShow(options) {
    // 从后台被 scheme 重新打开
    // options.query == {number:1}
  },

  getNotice() {
    return new Promise(async (resolve, reject) => {
      if (this.globalData.noti != null) return resolve(this.globalData.noti);
      const res = await other.home()
      this.globalData.noti = JSON.parse(res.data.noticeSlideShows)
      resolve(this.globalData.noti);
    })

  },


  // 小程序更新问题
  UpdateApp() {
    const updateManager = my.getUpdateManager()
    updateManager.onCheckForUpdate(function (res) { // 请求完新版本信息的回调  
      console.log("======>>版本信息hasUpdate", res)
    })
    updateManager.onUpdateReady(function () {
      my.confirm({
        title: '更新提示',
        content: '新版本已经准备好，是否重启应用？',
        success: function (res) {
          if (res.confirm) { // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      })
    })
    updateManager.onUpdateFailed(function () {
      // 新版本下载失败
    })
  },

  // 封装  ： 获取地理位置
  getLocation() {
    if (this.globalData.userlocation) return false;
    const userlocation = storage('userlocation')
    if (userlocation) {
      this.globalData.userlocation = userlocation
      return false;
    }
    let that = this
    my.getLocation({
      type: 1,
      success(res) {
        console.log("=====================>>获取地理位置")
        console.log(res)
        that.globalData.userlocation = `${res.province}-${res.city}-${res.district}`
        console.log("11111")
      },
      fail(err) {
        console.log(err)
        switch (err.error) {
          case 2001:
            that.globalData.userlocation = that.globalData.userlocation || '用户拒绝访问当前地址(用户选择拒绝)';
            break;
          case 2002:
            that.globalData.userlocation = that.globalData.userlocation || '用户拒绝访问当前地址(之前设置默认拒绝)';
            break;
          case 2002:
            that.globalData.userlocation = that.globalData.userlocation || '用户拒绝访问当前地址(本次设置默认拒绝)';
            break;
        }

        console.log("22222")
      },
      complete() {
        storage('userlocation', that.globalData.userlocation)
      }

    })

  },

  // 支付
  alipay(data) {
    return new Promise((resolve, reject) => {
      my.tradePay({
        tradeNO: data,
        success: function (res) {
          console.log(res)
          if (res.resultCode == 9000) {
            resolve(res)
          } else {
            reject(res)
          }
        },
      });

    })
  },



  // 设置路由
  _init_(that) {
    that.setData({
      url: URL
    })
  },

  _getProductIdList(that) {
    that.product.getProdActIdList().then(res => {
      that.globalData.productId = res.data
    })
  },

  // 跳转
  navigateTo(e) {
    routes.navigateTo(e.target.dataset);
  },

  editTabbar: function () {
    let tabbar = this.globalData.tabBar;
    console.log('666', tabbar)
    let currentPages = getCurrentPages();
    let _this = currentPages[currentPages.length - 1];
    let pagePath = _this.route;
    (pagePath.indexOf('/') != 0) && (pagePath = '/' + pagePath);
    for (let i in tabbar.list) {
      tabbar.list[i].selected = false;
      (tabbar.list[i].pagePath == pagePath) && (tabbar.list[i].selected = true);
    }
    _this.setData({
      tabbar: tabbar
    });
  },

});